<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <!-- <el-form-item label="医院" prop="hospitalId">
        <el-input v-model="formData.hospitalId" placeholder="请输入医院" />
      </el-form-item> -->
      <el-form-item label="职位名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入职位名称" />
      </el-form-item>
      <!-- <el-form-item label="职位类型" prop="type">
        <el-select v-model="formData.type" placeholder="请选择职位类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="职位简介" prop="description">
        <el-input
          v-model="formData.description"
          type="textarea"
          :autosize="{ minRows: 5, maxRows: 10 }"
          placeholder="请输入职位简介"
        />
      </el-form-item>
      <el-form-item label="工作地点" prop="jobPlace">
        <el-input v-model="formData.jobPlace" placeholder="请输入工作地点" />
      </el-form-item>
      <el-form-item label="最低薪水" prop="miniSalary">
        <el-input v-model="formData.miniSalary" placeholder="请输入最低薪水" />
      </el-form-item>
      <el-form-item label="最高薪水" prop="maxSalary">
        <el-input v-model="formData.maxSalary" placeholder="请输入最高薪水" />
      </el-form-item>
      <el-form-item label="学历要求" prop="education">
        <el-select v-model="formData.education" placeholder="请选择学历要求">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.HOSPITAL_RECRUITMENT_EDUCATION_LEVEL)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任职要求" prop="jobRequire">
        <el-input
          v-model="formData.jobRequire"
          type="textarea"
          :autosize="{ minRows: 5, maxRows: 10 }"
          placeholder="请输入任职要求"
        />
      </el-form-item>
      <el-form-item label="职位标签" prop="jobTag">
        <el-checkbox-group v-model="formData.jobTag">
          <el-checkbox
            v-for="dict in getStrDictOptions(DICT_TYPE.HOSPITAL_RECRUITMENT_POSITION_TAG)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="招聘人数" prop="number">
        <el-input v-model="formData.number" placeholder="请输入招聘人数" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- <el-form-item label="租户编号" prop="tenantId">
        <el-input v-model="formData.tenantId" placeholder="请输入租户编号" />
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { RecruitmentPositionApi, RecruitmentPositionVO } from '@/api/hospital/recruitment.position'

/** 职位 表单 */
defineOptions({ name: 'RecruitmentPositionForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  hospitalId: undefined,
  name: undefined,
  type: undefined,
  number: undefined,
  miniSalary: undefined,
  maxSalary: undefined,
  jobPlace: undefined,
  education: undefined,
  jobRequire: undefined,
  jobTag: [],
  description: undefined,
  status: undefined,
  tenantId: undefined
})
const formRules = reactive({
  status: [{ required: true, message: '状态不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  // dialogTitle.value = t('action.' + type)

  if (type === 'create') {
    dialogTitle.value = '发布新招聘'
  } else {
    dialogTitle.value = '编辑招聘信息'
  }

  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      // formData.value = await RecruitmentPositionApi.getRecruitmentPosition(id)
      const good = await RecruitmentPositionApi.getRecruitmentPosition(id);
      good.jobTag = good.jobTag.split(',');
      console.log(good)

      formData.value = good;


    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    // formData.value.jobTag = []
    const data = formData.value as unknown as RecruitmentPositionVO;

    const data2 = JSON.parse(JSON.stringify(data));
    data2.jobTag = data2.jobTag.toString()

    if (formType.value === 'create') {
      await RecruitmentPositionApi.createRecruitmentPosition(data2)
      message.success(t('common.createSuccess'))
    } else {
      await RecruitmentPositionApi.updateRecruitmentPosition(data2)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    hospitalId: undefined,
    name: undefined,
    type: undefined,
    number: undefined,
    miniSalary: undefined,
    maxSalary: undefined,
    jobPlace: undefined,
    education: undefined,
    jobRequire: undefined,
    jobTag: [],
    description: undefined,
    status: undefined,
    tenantId: undefined
  }
  formRef.value?.resetFields()
}
</script>